#include "decompress.h"
#include "video.h"
#include "plcsystem.h"
#include "plc.h"


word bufindex;
word exindex;
word grpindex;
word f_plc_execute;
extern PLCindex ArtLoadCues[];
extern word *vRam;
plc_buffer_index v_plc_buffer[0xFF];      //0x60 bytes...



void AddPLC (word index)
{
PLCindex *ArtLoadCues2=ArtLoadCues;
	//index=index*2; for words
    bufindex=0;
    grpindex=0;
	PLCindex curPLCGroup=ArtLoadCues2[index];
	word length = curPLCGroup.length;

        while (v_plc_buffer[bufindex+exindex].val1!=0) { bufindex++; }
		while (length > 0)
		{
            
			v_plc_buffer[bufindex+exindex].val1=curPLCGroup.plc[grpindex].graphics;
			v_plc_buffer[bufindex+exindex].val2=curPLCGroup.plc[grpindex].vram;
            
			bufindex=bufindex+1;
                        grpindex=grpindex+1;
			length=length-1;
		}
	//}
}

void NewPLC (word index)	//initializes pattern load cue buffer with PLC at index in ArtLoadCues
{
PLCindex *ArtLoadCues2=ArtLoadCues;
	//index=index*2; for words
	bufindex=0;
        grpindex=0;
	PLCindex curPLCGroup=ArtLoadCues2[index];
	word length = curPLCGroup.length;
	
	ClearPLCs();   //erase v_plc_buffer
	
	while (length > 0)
	{

		v_plc_buffer[bufindex+exindex].val1=(byte*)curPLCGroup.plc[grpindex].graphics;
		v_plc_buffer[bufindex+exindex].val2=curPLCGroup.plc[grpindex].vram;            
		bufindex=bufindex+1;
                grpindex=grpindex+1;
		length=length-1;
	}
	
}

void ClearPLCs()      //clears the pattern load cue buffer
{ 
    f_plc_execute=0;
	for (int bindex=0; bindex < 31; bindex++)
	{
		v_plc_buffer[bindex+exindex].val1=0;
		v_plc_buffer[bindex+exindex].val2=0;
	}
	bufindex=0;
	//exindex=0;
}

void RunPLCs()
{
     //exindex=0;
     while ((v_plc_buffer[exindex].val1!=0)){ RunPLC(); }
}

void RunPLC()
{
     //exindex=bufindex-1; this is if we start at end of list
     
     int *loc_1502;
	
     if (v_plc_buffer[exindex].val1!=0) 
     {
		//if (f_plc_execute==0)
		//{
            
            int offset=1;
			//word d2=v_plc_buffer[bufindex].val1;
			//if (d2 <= 0)
			 //  loc_1502+=10;
			 //TODO: WORRY ABOUT THESE INSTRUCTIONS LATER
        
           		
            
//incrementing a0 to skip the first word half at source position for nem decompression to not waste time checksif 0
  //move.w	(a0)+,d2//
//		bpl.s	loc_160E
//		adda.w	#$A,a3

//loc_160E:
//		andi.w	#$7FFF,d2
//		move.w	d2,(f_plc_execute).w
        
        
			//f_plc_execute=d2 & 0x7FFF;
		    f_plc_execute=0;  //for now?

            NemDec(v_plc_buffer[exindex].val1,(byte*)&vRam[v_plc_buffer[exindex].val2]); //destination is v_ngfx_buffer in rom, is this a directcopy?
		    
			 
			word val1=(v_plc_buffer[exindex].val2 & 0xFF00);
			word val2=(v_plc_buffer[exindex].val2 & 0x00FF);
			word d5 = val1+val2;                             //these calcs are incorrect
			
			//$FFFFF6E4.w = 0
		   //$FFFFF6E8.w = 0
		   //$FFFFF6EC.w = 0 
		    int FFFFF6F0w = d5;
		    
		   //$FFFFF6F4.w = count = 10
			word count=10;
    	
            //v_plc_buffer+=6;		//next cue in buffer..., 6 bytes in rom or
            exindex=exindex+1;
			
		
		//}
	}
}

bool Exists()
{
	return (v_plc_buffer[0].val1 != 0);
}
